'// ========================================================================= '
'// ------------------------------------------------------------------------- '
'//!				\file		CApp.vb
'			        \date		june 2010
'			        \author		TNick
'
'			        \brief		
'
'
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'   This file is considered universal knowledge. No restrictions
'   are imposed; use as you see fit.
'   Please read ReadMe.txt and Licence.txt in root folder
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'
'   The code here implements general application code such as initialization
'   and termination
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
'//
'//
'//
'//
'//  IMPORTS     ------------------------------------------------------------ '
#Region "Imports"
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.IO
Imports System.Runtime.InteropServices

Imports Autodesk.AutoCAD

#End Region
'//  IMPORTS     ============================================================ '
'//
'//
'//
'//
'//  DEFINITIONS    --------------------------------------------------------- '

    Public Structure SHFILEINFO
        Public hIcon As IntPtr            ' : icon
        Public iIcon As Integer           ' : icondex
        Public dwAttributes As Integer    ' : SFGAO_ flags
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
        Public szDisplayName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
        Public szTypeName As String
    End Structure

    Public structure POINTAPI
      dim X As Long
      dim Y As Long
    End structure

'//  DEFINITIONS    ========================================================= '
'//
'//
'//
'//
'//  DATA    ---------------------------------------------------------------- '


'//  DATA    ================================================================ '
'//
'//
'//
'//
'//  CLASS    --------------------------------------------------------------- '

Public Class CD_ImpEx

    '// --------------------------------------------------------------------- '
    '// DATA
    Private bImp As Boolean = False
    Private cBs As CM_3DPoints
    '// Indicates if we are changing the selected node of 
    '// the treeview programmatically
    Private ChangingSelectedNode As Boolean
   '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    '// API's
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

    Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" _
            (ByVal pszPath As String, _
             ByVal dwFileAttributes As Integer, _
             ByRef psfi As SHFILEINFO, _
             ByVal cbFileInfo As Integer, _
             ByVal uFlags As Integer) As IntPtr

    Private Const SHGFI_ICON As Long = &H100
    Private Const SHGFI_SMALLICON As Long = &H1
    Private Const SHGFI_LARGEICON As Long = &H0    ' Large icon
    Private Const MAX_PATH As Long = 260
    '// ===================================================================== '


    Const EXP_TXT As Integer = 0
    Const EXP_XL As Integer = 1
    Const EXP_CLPB As Integer = 2




    ' /////////////////////////////////////////////////////////////////////////
    #Region "Private functions"

    '// --------------------------------------------------------------------- '
    Private Sub AddImages(ByVal strFileName As String)

        Dim shInfo As SHFILEINFO
        shInfo = New SHFILEINFO()
        shInfo.szDisplayName = New String(CChar(vbNullChar), MAX_PATH)
        shInfo.szTypeName = New String(CChar(vbNullChar), 80)
        Dim hIcon As IntPtr
        hIcon = SHGetFileInfo(strFileName, 0, shInfo, Marshal.SizeOf(shInfo), SHGFI_ICON Or SHGFI_SMALLICON)
        Dim MyIcon As Drawing.Bitmap
        MyIcon = Drawing.Icon.FromHandle(shInfo.hIcon).ToBitmap
        ExplImgLst.Images.Add(strFileName.ToString(), MyIcon)

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub AddAllFolders(ByVal TNode As TreeNode, ByVal FolderPath As String)
        Try
            For Each FolderNode As String In Directory.GetDirectories(FolderPath)
                Dim SubFolderNode As TreeNode = TNode.Nodes.Add(FolderNode.Substring(FolderNode.LastIndexOf("\"c) + 1))

                SubFolderNode.Tag = FolderNode

                SubFolderNode.Nodes.Add("Loading...")

            Next
        Catch ex As Exception
            CMain.DebugException(ex)
        End Try

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub GetAllDrives()
        Dim allDrives() As DriveInfo = DriveInfo.GetDrives()

        Dim d As DriveInfo
        For Each d In allDrives
            try
                Dim Tnode As TreeNode = _
                    TW_Folders.Nodes.Add(d.VolumeLabel & " (" & d.Name & ")")
                AddAllFolders(Tnode, d.Name)
                Tnode.Tag = d.Name
            Catch
            End try
        Next
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub SetUpListViewColumns()
        ' TODO: Add code to set up listview columns

        LW_Files.View = View.Details
        ' Add a column with width 80 and left alignment
        LW_Files.Columns.Add(CMain._T(TX.NAME), 150, HorizontalAlignment.Left)
        LW_Files.Columns.Add(CMain._T(TX.TYPE), 80, HorizontalAlignment.Left)
        LW_Files.Columns.Add(CMain._T(TX.MODIFIED), 150, HorizontalAlignment.Left)
        SetView(View.Details)

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    'Change whether or not the folders pane is visible
    Private Sub ToggleFoldersVisible()

        'Change the Folders toolbar button to be in sync
        FoldersToolStripButton.Checked = Not FoldersToolStripButton.Checked

        ' Collapse the Panel containing the TreeView.
        Me.SplitContainer.Panel1Collapsed = Not FoldersToolStripButton.Checked
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub SetView(ByVal View As System.Windows.Forms.View)
        'Figure out which menu item should be checked
        Dim MenuItemToCheck As ToolStripMenuItem = Nothing
        Select Case View
            Case View.Details
                MenuItemToCheck = DetailsToolStripMenuItem
            Case View.LargeIcon
                MenuItemToCheck = LargeIconsToolStripMenuItem
            Case View.List
                MenuItemToCheck = ListToolStripMenuItem
            Case View.SmallIcon
                MenuItemToCheck = SmallIconsToolStripMenuItem
            Case View.Tile
                MenuItemToCheck = TileToolStripMenuItem
            Case Else
                Debug.Fail("Unexpected View")
                View = View.Details
                MenuItemToCheck = DetailsToolStripMenuItem
        End Select

        'Check the appropriate menu item and deselect all others under the Views menu
        For Each MenuItem As ToolStripMenuItem In ListViewToolStripButton.DropDownItems
            If MenuItem Is MenuItemToCheck Then
                MenuItem.Checked = True
            Else
                MenuItem.Checked = False
            End If
        Next

        'Finally, set the view requested
        LW_Files.View = View
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Function Closure (ByRef sFile As String) As Boolean
        Dim i As Integer
        Dim lcSTG As New PT3DSTG

        Dim ExpTy As Integer
        ExpTy = EXP_TXT
        If (sFile.Length > 4) then
            If ((sFile.Substring(sFile.Length-4)).ToUpper() = ".XLS") Then
                ExpTy = EXP_XL
            ElseIf sFile.ToUpper() = CM_3DPoints.sCLIPB Then
                ExpTy = EXP_CLPB
            End If
        ElseIf (sFile.Length = 1) then
            If sFile.ToUpper() = "C" Then
                ExpTy = EXP_CLPB
                sFile = CM_3DPoints.sCLIPB
            End If
        End If


        ' create a local copy of global settings
        lcSTG = CM_3DPoints.Stgs

        If (ExpTy <> EXP_XL) Then
            lcSTG.DecSep = Me.TX_DecSep.Text
            If (LS_Sep.Items.Count = 0)
                MsgBox(CMain._T(TX.PT3D_EFLD))
                Return False
            Else
                ReDim lcSTG.FSep(0 to LS_Sep.Items.Count - 1)
                For i = 0 to LS_Sep.Items.Count - 1
                    lcSTG.FSep(i) = _
                        CM_3DPoints.GetSepFromStr(LS_Sep.Items(i).ToString())
                Next
            End If
            If (lcSTG.DecSep.Length = 0) Then
                CMain.AlertUser(TX.PT3D_EDEC)
                Return False
            End if
            If (lcSTG.FSep Is Nothing Or UBound(lcSTG.FSep) = -1) Then
                CMain.AlertUser(TX.PT3D_EFLD)
                Return False
            End if
        End if   

        For i = 0 to LS_Order.Items.Count - 1
            lcSTG.FOrd(i) = CM_3DPoints.GetOrdFormStr(LS_Order.Items(i).ToString())
        Next i

        If (bImp = True)
            If (ExpTy <> EXP_CLPB ) AndAlso (Not (system.IO.File.Exists(sFile)))
                '// not alert user here, since this is an individual dialog
                Call MsgBox(CMain._T(TX.ERR_FILE) &  "!!!" & vbNewLine & _
                            sFile,MsgBoxStyle.Critical,Me.Text)
            Else
                Me.Close()
                'Dim FpNr As Long = 100
                'If (IsNumeric(Me.TX_PtNr.Text))
                '    FpNr = CLng(Me.TX_PtNr.Text)
                '    If FpNr < 0 Then
                '        FpNr = 100
                '        Me.TX_PtNr.Text = "100"
                '    End If
                'Else
                '    Me.TX_PtNr.Text = "100"
                'End If
                    CM_3DPoints.Stgs.LastImpFile = sFile
                Return CM_3DPoints.ImportFile(sFile, lcSTG)
            End If
        Else
            CM_3DPoints.Stgs.LastExpFile = sFile
            Return cBs.ExportFile(sFile, lcSTG)
        End If
        Return False
    End Function
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub GotoFolder (ByRef sFolder As String)
        Dim Parts() As String = Nothing
        Dim SepList(0) As string
        Dim PartCnt As Integer
        Dim folder As String
        Dim tNode As System.Windows.Forms.TreeNode
        Dim tNodeIn As System.Windows.Forms.TreeNode
        Dim i As Integer

        SepList(0) = "\"
        PartCnt = CMain.Ln2Tk(sFolder,Parts,SepList)
        If (PartCnt=0) Then Return
        For i = 0 to PartCnt - 1
            Parts(i) = Parts(i).ToUpper
        Next
        folder = Parts(0) & "\"
        For Each tNode In TW_Folders.Nodes
            If (tNode.Tag.ToString.ToUpper() = folder) Then
                TW_Folders.SelectedNode = tNode
                TW_Folders.SelectedNode.Expand()
                i = 1
                While (i < PartCnt)
                    folder = folder & Parts(i) & "\"
                    For Each tNodeIn In TW_Folders.SelectedNode.Nodes
                        If (tNodeIn.Text.ToString.ToUpper() = Parts(i)) Then
                            TW_Folders.SelectedNode = tNodeIn
                            TW_Folders.SelectedNode.Expand()
                            GoTo NextLevel
                        End If
                    Next
                    Return
                    NextLevel:
                    i = i + 1
                End While
            End If
        Next

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub LoadCMSettings()
        Dim i As Integer
        For i = 0 to PT3PART.COUNT - 1
            LS_Order.Items.Add( _
                CM_3DPoints.GetStrFormOrd( _
                    CM_3DPoints.Stgs.FOrd(i) _
                                         ) _
                              )
        Next i

        If (Not CM_3DPoints.Stgs.FSep is Nothing andalso _
            Ubound(CM_3DPoints.Stgs.FSep) <> -1) Then

            For i = 0 to Ubound(CM_3DPoints.Stgs.FSep)
                LS_Sep.Items.Add( _
                    CM_3DPoints.GetSepStr( _
                        CM_3DPoints.Stgs.FSep(i)))
            Next
        End If

        Me.TX_DecSep.Text = CM_3DPoints.Stgs.DecSep

    End Sub
    '// ===================================================================== '

    #End Region
    ' /////////////////////////////////////////////////////////////////////////





    ' /////////////////////////////////////////////////////////////////////////
    #Region "Public functions"

    '// --------------------------------------------------------------------- '
    Public Sub SetModeImport
        bImp = True
        Me.BTN_OK.Text = CMain._T(TX.IMPORT)
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Public Sub SetModeExport
        bImp = False
        Me.BTN_OK.Text = CMain._T(TX.EXPORT)
    End Sub
    '// ===================================================================== '


    #End Region
    ' /////////////////////////////////////////////////////////////////////////





    ' /////////////////////////////////////////////////////////////////////////
    #Region "Event handlers"


    '// --------------------------------------------------------------------- '
    Private Sub Explorer1_Load(ByVal sender As Object, _
                               ByVal e As System.EventArgs) Handles Me.Load
        'Set up the UI
        TW_Folders.Sort()
        SetUpListViewColumns()
        GetAllDrives()

        LoadCMSettings()

        If (bImp)
            If Not (CM_3DPoints.Stgs.LastImpFile Is Nothing) Then
                Me.TX_CrtFile.Text = CM_3DPoints.Stgs.LastImpFile
                GotoFolder(CM_3DPoints.Stgs.LastImpFile)
            End If
        Else
            If Not (CM_3DPoints.Stgs.LastExpFile Is Nothing) Then
                Me.TX_CrtFile.Text = CM_3DPoints.Stgs.LastExpFile
                GotoFolder(CM_3DPoints.Stgs.LastExpFile)
            End If
        End If
        Me.L_Warning.Text = CMain._T(TX.WARNING) & "!!! " & _
            CMain._T(TX.STGUNCHG) & "->"

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_SepAdd_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_SepAdd.Click
        
        Dim sTmp As String, i As Integer
        sTmp = Me.TX_NewSep.Text
        If (sTmp.Length = 0)
            MsgBox(CMain._T(TX.ERR_EMTX))
            Return
        End If
        If (sTmp = " ") Then
            sTmp = "<" & CMain._T(TX.SPACE) & ">"
        ElseIf (sTmp = Chr(9)) Then
            sTmp = "<TAB>"
        End If
        For i = 0 to Me.LS_Sep.Items.Count - 1
            If (sTmp = Me.LS_Sep.Items(i).ToString) Then
                MsgBox(CMain._T(TX.PT3D_EDUPL))
                Return
            End If
        Next i
        Me.LS_Sep.Items.Add(sTmp)
        Me.TX_NewSep.Text = ""
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_SepRem_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_SepRem.Click
        
        If Me.LS_Sep.SelectedIndex <> -1
            Dim i As Integer = Me.LS_Sep.SelectedIndex
            Me.LS_Sep.Items.RemoveAt(Me.LS_Sep.SelectedIndex)
            If (Me.LS_Sep.Items.Count > 0) Then
                If (i >= Me.LS_Sep.Items.Count) Then
                    Me.LS_Sep.SelectedIndex = Me.LS_Sep.Items.Count-1
                Else
                    Me.LS_Sep.SelectedIndex = i
                End If
            End if
        End If

    End Sub
    '// ===================================================================== '


    '// --------------------------------------------------------------------- '
    Private Sub ListToolStripMenuItem_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles ListToolStripMenuItem.Click

        SetView(View.List)
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub DetailsToolStripMenuItem_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles DetailsToolStripMenuItem.Click

        SetView(View.Details)
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub LargeIconsToolStripMenuItem_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles LargeIconsToolStripMenuItem.Click

        SetView(View.LargeIcon)
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub SmallIconsToolStripMenuItem_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles SmallIconsToolStripMenuItem.Click

        SetView(View.SmallIcon)
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub TileToolStripMenuItem_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles TileToolStripMenuItem.Click

        SetView(View.Tile)
    End Sub
    '// ===================================================================== '


    '// --------------------------------------------------------------------- '
    Private Sub TreeView_AfterSelect( _
        ByVal sender As Object, _
        ByVal e As System.Windows.Forms.TreeViewEventArgs) _
        Handles TW_Folders.AfterSelect

        Dim FileExtension As String
        Dim SubItemIndex As Integer
        Dim DateMod As String

        LW_Files.Items.Clear()

        If TW_Folders.SelectedNode.Nodes.Count = 1 AndAlso TW_Folders.SelectedNode.Nodes(0).Text = "Loading..." Then

            TW_Folders.SelectedNode.Nodes.Clear()

            AddAllFolders(TW_Folders.SelectedNode, CStr(TW_Folders.SelectedNode.Tag))

        End If
        Dim folder As String = CStr(TW_Folders.SelectedNode.Tag)

        If Not folder Is Nothing AndAlso IO.Directory.Exists(folder) Then
            Try
                For Each folder_K As String In IO.Directory.GetDirectories(folder)
                    FileExtension = CMain._T(TX.FOLDER)
                    DateMod = IO.File.GetLastWriteTime(folder_K).ToString()

                    AddImages(folder_K)

                    LW_Files.Items.Add(folder_K.Substring(folder_K.LastIndexOf("\"c) + 1), folder_K.ToString())
                    LW_Files.Items(SubItemIndex).SubItems.Add(FileExtension.ToString())
                    LW_Files.Items(SubItemIndex).SubItems.Add(DateMod.ToString())
                    SubItemIndex += 1

                Next
                For Each file As String In IO.Directory.GetFiles(folder)
                    FileExtension = IO.Path.GetExtension(file)
                    DateMod = IO.File.GetLastWriteTime(file).ToString()

                    AddImages(file)

                    LW_Files.Items.Add(file.Substring(file.LastIndexOf("\"c) + 1), file.ToString())
                    LW_Files.Items(SubItemIndex).SubItems.Add(FileExtension.ToString())
                    LW_Files.Items(SubItemIndex).SubItems.Add(DateMod.ToString())
                    SubItemIndex += 1

                Next
            Catch ex As Exception
                CMain.DebugException(ex)
            End Try
        End If
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub TW_Folders_BeforeExpand( _
        ByVal sender As Object, _
        ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) _
        Handles TW_Folders.BeforeExpand

        If e.Node.Nodes.Count = 1 AndAlso e.Node.Nodes(0).Text = "Loading..." Then

            e.Node.Nodes.Clear()
            AddAllFolders(e.Node, CStr(e.Node.Tag))

        End If
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub LW_Files_SelectedIndexChanged( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles LW_Files.SelectedIndexChanged

        Dim folder As String = CStr(TW_Folders.SelectedNode.Tag)
        Dim item As ListViewItem
        If Not folder Is Nothing AndAlso IO.Directory.Exists(folder) then
            If not folder.EndsWith("\") then
                folder = folder & "\"
            End If
            If (LW_Files.SelectedIndices.Count > 0)
                item = Me.LW_Files.SelectedItems(0)
                TX_CrtFile.Text = folder & item.Text
            End If
        End If
    End Sub
    '// ===================================================================== '


    '// --------------------------------------------------------------------- '
    Private Sub BTN_OK_Click( ByVal sender As System.Object,  _
                              ByVal e As System.EventArgs) _
                              Handles BTN_OK.Click
        Closure(Me.TX_CrtFile.Text)
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub LW_Files_MouseDoubleClick( _
        ByVal sender As System.Object,  _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles LW_Files.MouseDoubleClick

        Dim folder As String = CStr(TW_Folders.SelectedNode.Tag)
        Dim item As ListViewItem
        If Not folder Is Nothing AndAlso IO.Directory.Exists(folder) then
            If not folder.EndsWith("\") then
                folder = folder & "\"
            End If
            If (LW_Files.SelectedIndices.Count > 0)
                item = Me.LW_Files.SelectedItems(0)
                If (IO.Directory.Exists(folder & item.Text)) Then
                    ' we have to enter a folder
                    Dim i As Integer
                    For i = 0 to TW_Folders.SelectedNode.Nodes.Count -1
                        If (TW_Folders.SelectedNode.Nodes(i).Text = item.Text)
                            TW_Folders.SelectedNode = TW_Folders.SelectedNode.Nodes(i)
                            TW_Folders.SelectedNode.Expand()
                            return
                        End If
                    Next
                Else
                    ' We have a file
                    Closure(folder & item.Text)
                End If
            End If
        End If
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_LOrd_Up_Click( ByVal sender As System.Object, _
                                 ByVal e As System.EventArgs) _
                                 Handles B_LOrd_Up.Click
        
        Dim mSelectedIndex as integer
        Dim mOtherIndex as integer
        'do nothing if the top item is selected.
        If (Me.LS_Order.SelectedIndex = -1) Then Exit Sub
        If Me.LS_Order.SelectedIndex <> 0 Then
            mSelectedIndex = Me.LS_Order.SelectedIndex
            mOtherIndex = mSelectedIndex - 1
            LS_Order.Items.Insert(mSelectedIndex + 1, _
                                  LS_Order.Items(mOtherIndex))
            LS_Order.Items.RemoveAt(mOtherIndex)
            Me.LS_Order.SelectedIndex = mSelectedIndex-1
        End If
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_LOrd_Down_Click( ByVal sender As System.Object, _
                                   ByVal e As System.EventArgs) _
                                   Handles B_LOrd_Down.Click
        
        Dim mSelectedIndex as integer
        Dim mOtherIndex as integer
        'do nothing if the bottom item is selected.
        If (Me.LS_Order.SelectedIndex = -1) Then Exit Sub
        If Me.LS_Order.SelectedIndex <> Me.LS_Order.Items.Count - 1 Then
            mSelectedIndex = Me.LS_Order.SelectedIndex
            mOtherIndex = mSelectedIndex + 1
            LS_Order.Items.Insert(mSelectedIndex, LS_Order.Items(mOtherIndex))
            LS_Order.Items.RemoveAt(mOtherIndex + 1)
            Me.LS_Order.SelectedIndex = mSelectedIndex+1
        End If
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub FoldersToolStripButton_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles FoldersToolStripButton.Click

        ToggleFoldersVisible()
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub BTN_Cancel_Click( ByVal sender As System.Object,  _
                                  ByVal e As System.EventArgs) _
                                  Handles BTN_Cancel.Click
        Me.Close()
    End Sub
    '// ===================================================================== '


    '// --------------------------------------------------------------------- '
    Private Sub TX_CrtFile_KeyDown( _
        ByVal sender As System.Object,  _
        ByVal e As System.Windows.Forms.KeyEventArgs) _
        Handles TX_CrtFile.KeyDown
        
        If e.KeyCode = Keys.Enter Then
            e.SuppressKeyPress = True
            If (System.IO.File.Exists(Me.TX_CrtFile.Text)) Then
                Closure(Me.TX_CrtFile.Text)
            ElseIf (System.IO.Directory.Exists(Me.TX_CrtFile.Text)) Then
                GotoFolder(Me.TX_CrtFile.Text)
            Else
                If (bImp = False)
                    Closure(Me.TX_CrtFile.Text)
                Else
                    MsgBox(CMain._T(TX.NOTFILEFLD))
                End If
            End If
        End If

    End Sub
    '// ===================================================================== '


    #End Region
    ' /////////////////////////////////////////////////////////////////////////




    '// --------------------------------------------------------------------- '
    Public Sub New  (ByRef cBsRef As CM_3DPoints, ByVal bImporting As Boolean)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        Me.Text = CMain._T(TX.SELFILE) & ":"

        bImp = bImporting
        If (bImp) then
            Me.BTN_OK.Text = CMain._T(TX.IMPORT)
        Else
            Me.BTN_OK.Text = CMain._T(TX.EXPORT)
        End If
        Me.BTN_Cancel.Text = CMain._T(TX.CANCEL)

        Me.GRP_Sep.Text = CMain._T(TX.FLDSEP)
        Me.GRP_Cont.Text = CMain._T(TX.CONTENT)
        Me.B_SepAdd.Text = CMain._T(TX.ADD)
        Me.B_SepRem.Text = CMain._T(TX.REMOVE)
        Me.L_DecSep.Text = CMain._T(TX.DECSEP)
        Me.L_PtNr.Text = CMain._T(TX.PTNR)
        Me.TX_PtNr.Text = "100"
        cBs = cBsRef
    End Sub
    '// ===================================================================== '


    '// --------------------------------------------------------------------- '
    Private Sub B_Stgs_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_Stgs.Click

        Dim StgD As New CST_3DPoints
        If (ApplicationServices.Application.ShowModalDialog(StgD) = _
            System.Windows.Forms.DialogResult.OK) Then

            LS_Sep.Items.Clear()
            LS_Order.Items.Clear()
            Call LoadCMSettings
        End If

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub B_Browse_Click( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles B_Browse.Click

        If bImp Then
            Dim fdlg As OpenFileDialog = New OpenFileDialog()

            fdlg.Title = Me.Text
            fdlg.Filter = CMain._T(tx.EXCFILES) & " (*.xls)|*.xls|" & _
                          CMain._T(tx.TXTFILES) & " (*.txt)|*.txt|" & _
                          CMain._T(tx.ALLFILES) & " (*.*)|*.*"
            fdlg.FilterIndex = 2
            fdlg.RestoreDirectory = False
            fdlg.FileName = Me.TX_CrtFile.Text
            fdlg.CheckFileExists = True
            If fdlg.ShowDialog() = DialogResult.OK Then
                Me.TX_CrtFile.Text = fdlg.FileName
            End If
        Else
            Dim fdlg As SaveFileDialog = New SaveFileDialog()

            fdlg.Title = Me.Text
            fdlg.Filter = CMain._T(tx.EXCFILES) & " (*.xls)|*.xls|" & _
                          CMain._T(tx.TXTFILES) & " (*.txt)|*.txt|" & _
                          CMain._T(tx.ALLFILES) & " (*.*)|*.*"
            fdlg.FilterIndex = 2
            fdlg.RestoreDirectory = False
            fdlg.FileName = Me.TX_CrtFile.Text
            If fdlg.ShowDialog() = DialogResult.OK Then
                Me.TX_CrtFile.Text = fdlg.FileName
            End If
        End If

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub CD_ImpEx_FormClosed( _
        ByVal sender As System.Object,  _
        ByVal e As System.Windows.Forms.FormClosedEventArgs) _
        Handles MyBase.FormClosed

        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Sub
    '// ===================================================================== '


end Class



'//  CLASS    =============================================================== '
'//
'//
'//
'//
'//  FUNCTIONS    ----------------------------------------------------------- '

'//  FUNCTIONS    =========================================================== '
'//
'//
'//
'//
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
